package com.example.tijianbackend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.tijianbackend.dto.SetmealDTO;
import com.example.tijianbackend.pojo.Checkitem;
import com.example.tijianbackend.pojo.Setmeal;
import com.example.tijianbackend.pojo.Setmealdetailed;
import com.example.tijianbackend.result.Result;
import com.example.tijianbackend.service.CheckitemService;
import com.example.tijianbackend.service.SetmealService;
import com.example.tijianbackend.service.SetmealdetailedService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: bong~
 * @description: TODO
 * @date: 2024/6/14 15:06
 * @version: 1.0
 */

@RestController
@Slf4j
@RequestMapping("/setmeal")
@Api(tags ="setmeal")
@CrossOrigin("*")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealdetailedService setmealdetailedService;

    @Autowired
    private CheckitemService checkitemService;


    @GetMapping("/list")
    @ApiOperation(value = "体检套餐列表",notes = "根据男女类型区分，男：1，女：0")
    public Result getSetmealList(@RequestParam("type") Integer type){

        System.out.println(type);

        List<SetmealDTO> ans=new ArrayList<>();

        QueryWrapper queryWrapper1= (QueryWrapper) new QueryWrapper().eq("type",type);
        List<Setmeal> setmealsList = setmealService.list(queryWrapper1);

        // 遍历套餐列表
        for(Setmeal setmeal:setmealsList){

            Integer smId=setmeal.getSmId();
            QueryWrapper queryWrapper2= (QueryWrapper) new QueryWrapper().eq("smId",smId);
            List<Setmealdetailed> setmealdetailedList=setmealdetailedService.list(queryWrapper2);

            List<Integer> ciIds=setmealdetailedList.stream()
                    .map(Setmealdetailed::getCiId)
                    .collect(Collectors.toList());



            QueryWrapper queryWrapper3=(QueryWrapper) new QueryWrapper().in("ciId",ciIds);

            System.out.println(ciIds);
            List<Checkitem> checkitemList=checkitemService.list(queryWrapper3);


            ans.add(new SetmealDTO(setmeal,checkitemList));

        }
        return Result.success(ans);
    }


}
